package com.coder.algorithm.offer_2;

import java.util.Locale;

/**
 * 求 1+2+…+n
 *
 * @author yuhushuan
 * @date 2020/7/4 01:14
 */
public class _64_1_Accumulate {
    private static class Temp {
        private static int N;
        private static int Sum;

        static {
            ++N;
            Sum += N;
            System.out.println("static");
        }

        private static void reset() {
            N = 0;
            Sum = 0;
        }

        private static int getSum() {
            return Sum;
        }
    }


    /**
     * 仿 c ，Java无效
     * @param n
     * @return
     */
    private static int sum_Solution1(int n) {
        Temp.reset();
        Temp[] a = new Temp[n];
        Temp.class.getName();
        return Temp.getSum();
    }

    private static int sum_Recursively(int n) {
        int result = 0;
        int temp = 0;
        boolean flag = (n > 0) && temp == (result = sum_Recursively(n - 1));
        result += n;
        return result;
    }


    public static void main(String[] args) {
        // 功能测试
        print(5);
        print(10);

        // 边界值测试
        print(0);
        print(1);


    }

    private static void print(int n) {
        System.out.println(String.format(Locale.getDefault(),
                "从 1 开始累加到 %d 的值为：", n));
        System.out.println("通过构造方法：" + sum_Solution1(n));
        System.out.println("通过递归：" + sum_Recursively(n));
        System.out.println();
    }


}
